<?php

namespace App\Admin\Controllers;

use App\model\Coustomer;
use App\model\Project;
use App\model\Source;
use App\model\Xiaofei;
use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Spatie\Permission\Models\Role;

class ProjectXiaoFeiController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('消费报表')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Xiaofei());
        $grid->model()
            ->orderBy('id','desc')
            ->groupBy(\DB::raw('DATE_FORMAT( created_at, "%Y-%m-%d" )' ),'project_id','source_id')
            ->select('project_id','source_id',\DB::raw('DATE_FORMAT( created_at, "%Y-%m-%d" ) as day'  ));
        $grid->day('日期');
        $grid->project_id('项目')->display(function($value){
            return Project::find($value)->name ?? '未知项目';
        });
        $grid->tuiguang('推广员')->display(function ($value){
          $customer= Coustomer::join('model_has_roles','model_has_roles.model_id','=','customer.writer_id')
                ->where('model_has_roles.role_id','=',7)
                ->where('project_id',$this->project_id)
                ->first()
                ;
           if ($customer){
             return  User::find($customer->writer_id)->name ?? '尚未推广员';
           }else{
               return '尚未推广员';
           }
        });

        $grid->source_id('渠道')->display(function($value){
            return Source::find($value)->name ?? '未知渠道';
        });
        $grid->rukeshu('入客数')->display(function($value){
            return 0;
        });

        $grid->xiaofei("消费")->display(function ($value){
            return 0;
        });
        $grid->xiaofei1("入客均价")->display(function ($value){
            return 0;
        });
        $grid->xiaofei1("已看现场")->display(function ($value){
            return 0;
        });
        $grid->xiaofei2("到场率")->display(function ($value){
            return 0;
        });
        $grid->xiaofei3("成交数")->display(function ($value){
            return 0;
        });
        $grid->xiaofei4("成交率")->display(function ($value){
            return 0.00.'%';
        });
        $grid->xiaofei4("成交均价")->display(function ($value){
            return 0;
        });
        $grid->disableActions();
        $grid->disableTools();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->email('Email');
        $show->password('Password');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->mobile('Mobile');
        $show->project_id('Project id');
        $show->address('Address');
        $show->avatar('Avatar');
        $show->is_passed('Is passed');
        $show->session_key('Session key');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        $form->text('name', 'Name');
        $form->email('email', 'Email');
        $form->password('password', 'Password');
        $form->text('remember_token', 'Remember token');
        $form->mobile('mobile', 'Mobile');
        $form->text('project_id', 'Project id');
        $form->text('address', 'Address');
        $form->image('avatar', 'Avatar');
        $form->number('is_passed', 'Is passed');
        $form->text('session_key', 'Session key');
        return $form;
    }
}
